package mobilizator.app.add;

import java.util.ArrayList;
import java.util.Vector;

import mobilizator.app.MobilizatorApp;
import mobilizator.app.R;
import mobilizator.app.data.Category;
import mobilizator.app.data.Cloth;
import mobilizator.app.data.ClothingAttribute;
import mobilizator.app.data.IOUtility;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageView;

public class AddClothActivity extends Activity {
	private static final int PICTURE_RESULT = 1;
	private EditText clothName, clothDesc;
	private Button takePhoto, addCloth;
	private ExpandableListView expandableListView;
	private boolean[][] tabB;
	private String[][] values;
	private String[] categories = { "Kategoria", "Przeznaczenie", "Inne" };
	private ArrayList<Category> cat = MobilizatorApp.mdb.getMainCategories();
	private ExpandableListAdapter expandableListAdapter;
	OnClickListener moClickListener = new OnClickListener() {

		public void onClick(View v) {
			System.out.println("JUUUUUUUUUUUU");
			int id = v.getId();

			switch (id) {
			case R.id.add_button:
				saveData();

				finish();
				break;
			case R.id.take_foto_button:
				Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				startActivityForResult(intent, PICTURE_RESULT);
				break;

			default:
				break;
			}

		}
	};

	// Gallery gal = null;
	ImageAdapter images = null;
	private Bitmap pic;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_cloth);

		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

		clothName = (EditText) findViewById(R.id.edit_name);
		clothDesc = (EditText) findViewById(R.id.edit_desc);

		takePhoto = (Button) findViewById(R.id.take_foto_button);
		takePhoto.setOnClickListener(moClickListener);

		addCloth = (Button) findViewById(R.id.add_button);
		addCloth.setOnClickListener(moClickListener);

		values = new String[categories.length][];
		values[0] = new String[cat.size()];

		for (int i = 0; i < cat.size(); i++)
			values[0][i] = cat.get(i).getName();

		values[1] = new String[] { "Na dwor", "Do biura", "Na basen" };
		values[2] = new String[] { "Stare", "Znoszone", "Wygodne" };

		expandableListAdapter = new ExpandableListAdapter(this, categories,
				values);

		expandableListView = (ExpandableListView) findViewById(R.id.list);
		expandableListView.setAdapter(expandableListAdapter);

		images = new ImageAdapter(this);

		// gal = (Gallery) findViewById(R.id.gallery);
		// gal.setAdapter(images);

		clothName.requestFocus();

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == PICTURE_RESULT) {
			if (resultCode == Activity.RESULT_OK) {
				Bundle b = data.getExtras();
				pic = (Bitmap) b.get("data");

				// if (pic != null) {
				// images.AddImage(pic);
				// gal.setAdapter(images); // forces the gallery to add the new
				// images.
				// Vector<Bitmap> VImages =
				// for(B)
				// }
			}
		}
	}

	private class ImageAdapter extends BaseAdapter {
		private Context mContext;
		private Vector<Bitmap> images = new Vector<Bitmap>();

		public Vector<Bitmap> getBitmapVector() {
			return images;
		}

		public ImageAdapter(Context c) {
			System.out.println("ImageAdapter");
			mContext = c;
		}

		public void AddImage(Bitmap b) {
			System.out.println("AddImage");
			images.add(b);
		}

		public int getCount() {
			return images.size();
		}

		public Object getItem(int arg0) {
			return images.get(arg0);
		}

		public long getItemId(int arg0) {
			return arg0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView img;
			if (convertView == null) {
				img = new ImageView(mContext);
			} else {
				img = (ImageView) convertView;
			}

			img.setImageBitmap(images.get(position));
			return img;
		}

	}

	private void saveData() {
		ArrayList<Category> alc = new ArrayList<Category>();
		ArrayList<ClothingAttribute> alca = new ArrayList<ClothingAttribute>();
		String clothNameText = clothName.toString();
		Cloth cloth = new Cloth(clothNameText);
		String clothDescText = clothDesc.toString();
		cloth.setDescription(clothDescText);
		IOUtility.saveImageToSDCard(pic, clothNameText);
		tabB = expandableListAdapter.getCheckedItems();
		for (int i = 0; i < 3; ++i)
			for (boolean b : tabB[i]) {
				if (b) {
					switch (i) {
					case 0:
						alc.add(cat.get(i));
						break;

					case 1:
						alca.add(new ClothingAttribute(values[1][i]));
						break;

					case 2:
						alca.add(new ClothingAttribute(values[2][i]));
						break;

					default:
						break;
					}
				}
			}
		cloth.setCategories(alc);
		cloth.setAttribs(alca);
		MobilizatorApp.mdb.insertCloth(cloth);
	}

}
